﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.GameEventMechanics {
    import flash.events.*;
    import flash.display.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Events.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Display.*;
    import Modules.stats.experiments.*;
    import Modules.crew.*;
    import Modules.crew.ui.*;

    public class RollCallDialogIntroMechanic extends DialogGenerationMechanic implements IDialogGenerationMechanic {

        protected var m_pendingReopen:Boolean;
        protected var m_bSeenCollect:Boolean = false;

        public function RollCallDialogIntroMechanic(){
            this.m_pendingReopen = false;
            super();
        }
        override public function canPopDialog():Boolean{
            var _local1:RollCallDataMechanic = (MechanicManager.getInstance().getMechanicInstance(m_owner, "rollCall", MechanicManager.ALL) as RollCallDataMechanic);
            var _local2:int = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_ROLL_CALL);
            var _local3 = (Global.player.level < _local1.requiredLevel);
            if ((((((_local2 <= 1)) || (_local3))) && (!(Global.isVisiting())))){
                return (false);
            };
            var _local4:String = _local1.getState();
            var _local5 = !(super.canPopDialog());
            var _local6:Boolean = ((_local1.isActiveObject()) && ((_local4 == RollCallDataMechanic.STATE_IN_PROGRESS)));
            var _local7:Boolean = ((_local1.isActiveObject()) && ((((_local4 == RollCallDataMechanic.STATE_COMPLETE)) && (_local1.canCollect(Global.player.uid)))));
            var _local8:Boolean = ((_local1.isActiveObject()) && ((_local4 == RollCallDataMechanic.STATE_FINISHED)));
            if (_local1.isActiveObject()){
                if (((_local1.canPerformRollCall()) && (!(_local5)))){
                    RollCallManager.debugSample("IntroMechanicTrue", "canPerformNotHarvestable");
                    return (true);
                };
                if (((!(_local5)) && (((_local6) || (this.m_bSeenCollect))))){
                    this.makeRollCallDialog();
                    return (false);
                };
                if (((!(_local5)) && (_local7))){
                    if (!this.checkCollection()){
                        this.makeRollCallFailDialog();
                    } else {
                        this.makePigeonCongratsDialog();
                    };
                    this.m_bSeenCollect = true;
                    return (false);
                };
                if (((!(_local5)) && (_local8))){
                    return (false);
                };
            };
            return (false);
        }
        protected function isUpgradable():Boolean{
            var _local1:Boolean;
            var _local2:Municipal = (m_owner as Municipal);
            if (_local2){
                _local1 = _local2.getLogicComponent().passesExperimentGate();
            };
            return (((_local1) && (m_owner.isUpgradePossible())));
        }
        override public function instantiateDialog():DisplayObject{
            var _local2:DisplayObject;
            var _local1:Class = (getDefinitionByName(m_config.params["dialogToPop"]) as Class);
            _local2 = new _local1(m_owner, "", "", GenericDialogView.TYPE_CUSTOM_OK, this.dialogCloseCallback, "RollCallIntroDialog", "", false, 0, "", null, ZLoc.t("Dialogs", "RollCall_startrollcall"));
            Global.ui.displayLightbox(true, UI.MASK_ALL_UI, true);
            return (_local2);
        }
        private function makeRollCallDialog():void{
            var _local1:RollCallDataMechanic = (MechanicManager.getInstance().getMechanicInstance(m_owner, "rollCall", MechanicManager.ALL) as RollCallDataMechanic);
            var _local2:RollCallDialog = new RollCallDialog(m_owner, _local1, this.dialogCloseCallback);
            Global.ui.displayLightbox(true, UI.MASK_ALL_UI, true);
            UI.displayPopup(_local2);
        }
        private function makePigeonCongratsDialog():void{
            var _local1:GenericDialog;
            var _local2:String = ZLoc.t("Dialogs", "RollCall_pigeonRewards_text");
            _local1 = new CharacterResponseDialog(_local2, "RollCall_pigeonRewards", GenericDialogView.TYPE_CUSTOM_OK, this.pigeonCongratsCallback, "RollCall_pigeonRewards", "assets/dialogs/payroll_sam_happyPigeon.png", true, 0, "", null, ZLoc.t("Dialogs", "RollCall_pigeonRewards_button"));
            UI.displayPopup(_local1);
        }
        private function pigeonCongratsCallback(_arg1:GenericPopupEvent):void{
            if (_arg1.button == GenericDialogView.YES){
                this.makeRollCallDialog();
            };
        }
        private function makeRollCallFailDialog():void{
            var _local1:GenericDialog;
            var _local2:String = ZLoc.t("Dialogs", "RollCall_callFail_text");
            _local1 = new CharacterResponseDialog(_local2, "RollCall_callFail", GenericDialogView.TYPE_OK, this.rollCallFailCallback, "RollCall_callFail", "assets/dialogs/payroll_sam_happyPigeon.png");
            UI.displayPopup(_local1);
        }
        private function rollCallFailCallback(_arg1:GenericPopupEvent):void{
            var _local2:RollCallDataMechanic = (MechanicManager.getInstance().getMechanicInstance(m_owner, "rollCall", MechanicManager.ALL) as RollCallDataMechanic);
            _local2.setFinished();
        }
        private function checkCollection():Boolean{
            var _local5:Object;
            var _local6:Object;
            var _local7:int;
            var _local1:TieredDooberMechanic = (MechanicManager.getInstance().getMechanicInstance(m_owner, "rollCallTieredDooberValue", MechanicManager.ALL) as TieredDooberMechanic);
            var _local2:RollCallDataMechanic = (MechanicManager.getInstance().getMechanicInstance(m_owner, "rollCall", MechanicManager.ALL) as RollCallDataMechanic);
            var _local3:Array = _local2.getCrewState();
            var _local4:int;
            for each (_local5 in _local3) {
                if (_local5.checkedIn == true){
                    _local4++;
                };
            };
            _local6 = _local1.getTierInfo(1);
            _local7 = _local6.num;
            if (_local4 >= _local7){
                return (true);
            };
            return (false);
        }
        protected function dialogCloseCallback(_arg1:GenericPopupEvent):void{
            var _local2:RollCallDataMechanic;
            Global.ui.displayLightbox(false);
            m_owner.dispatchEvent(new Event(MapResource.REPLACE_CONTENT));
            if (_arg1.button == GenericDialogView.YES){
                _local2 = (MechanicManager.getInstance().getMechanicInstance(m_owner, "rollCall", MechanicManager.ALL) as RollCallDataMechanic);
                if (_local2.canCollect(Global.player.uid)){
                    if (!this.checkCollection()){
                        this.makeRollCallFailDialog();
                    } else {
                        this.makePigeonCongratsDialog();
                    };
                };
                if (_local2.canPerformRollCall()){
                    _local2.startRollCall();
                    this.m_pendingReopen = true;
                    this.makeRollCallDialog();
                };
            };
        }

    }
}//package Mechanics.GameEventMechanics 
